package com.cloud.business.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloud.business.entity.Account_businessInfo;
import com.cloud.business.service.businessService;
import com.cloud.commonapi.jsonResult.Result;
import com.cloud.commonapi.model.Business_info;
import com.cloud.commonapi.model.User_info;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * author : 黄永佳
 * create : 2019/9/26 0026 10:37
 * modified :
 * 功能 :
 **/
@Controller
@Api(description = "跑腿业务模块")
@RequestMapping("/business")
public class businessController {
    @Autowired
    businessService business;
    @Autowired
    RestTemplate restTemplate;
    @ApiOperation(value="新增跑腿业务")
    @PostMapping("/businesses")
    @ResponseBody
    public Result add(Business_info business_info,HttpSession session){
        Integer accountId=Integer.parseInt(session.getAttribute("loginId").toString());
        if(session.getAttribute("loginId")==null){
            return Result.build(400,"请登录");
        }
        JSONObject jSONObject= restTemplate.getForObject("http://USERCENTER/usercontroller/userinfo/users/"+accountId,JSONObject.class);
        JSONObject  data=jSONObject.getJSONObject("data");
        User_info user_info= JSON.parseObject(data.toString(), User_info.class);
        if(user_info.getCredit()<60){
            return Result.build(400,"信誉值不足，请前往个人中心进行实名认证");
        }
        business_info.setAccountId(accountId);

        if(business_info!=null){
            business_info.setStatus(1);
            business_info.setTime(new Timestamp(new Date().getTime()));
            int success=business.insertBusiness(business_info);
            if(success>0){
                return Result.build(200,"发布成功");
            }
            else{
                return Result.build(400,"发布失败");
            }
        }

        return Result.build(300,"参数错误");
    }
    @ApiOperation(value="根据id查看跑腿业务")
    @GetMapping("/{id}")
    @ResponseBody
    public Result select(@PathVariable int id){
        Account_businessInfo account_businessInfo=business.selectById(id);
        System.out.println(account_businessInfo.getAccountId());
        JSONObject jSONObject= restTemplate.getForObject("http://USERCENTER/usercontroller/userinfo/users/"+account_businessInfo.getAccountId(),JSONObject.class);
        JSONObject  data=jSONObject.getJSONObject("data");
        User_info user_info=JSON.parseObject(data.toString(), User_info.class);
        System.out.println(user_info.getUsername());
        //        System.out.println((User_info)R.getData());
//        User_info user_info=(User_info)R.getData();
//        System.out.println(user_info.getUsername());
        //List<User_info> user_infos = JSON.parseArray(json, User_info.class);
//        System.out.println(user_infos.toString());
       // System.out.println(user_infos.get(0).getUsername());
//        JSONObject jsonobject = JSONObject.fromObject(json);
//         User_info user= (User_info) JSONObject.toBean(object,User_info.class);
//        JSONObject jsonObject=JSONObject.parseObject(json);
        //Student stu=(Student)JSONObject.toBean(jsonObject, Student.class);
//        User_info user= (User_info) JSONObject.toJavaObject(jsonObject,User_info.class);
////       System.out.println(user.getUsername());
        if(account_businessInfo!=null){
            HashMap<String,Object> hash=new HashMap<>();
            hash.put("account_businessInfo",account_businessInfo);
            hash.put("userinfo",user_info);
            return Result.buildData(202,"查询成功",hash);
        }else {
            return Result.build(400,"查无此业务");
        }
    }
    @ApiOperation(value="查看跑腿业务列表")
    @GetMapping("/list")
    @ResponseBody
    public Result findAll(){
        List<Account_businessInfo> account_businessInfos=business.findAll();
        if(account_businessInfos!=null){
            HashMap<String,List<Account_businessInfo>> hashMap=new HashMap<>();
            hashMap.put("account_businessInfos",account_businessInfos);
            return Result.buildData(202,"查询成功",hashMap);
        }else {
            return Result.build(200,"查无数据");
        }
    }
    @ApiOperation(value = "搜索跑腿业务")
    @PostMapping("keyword")
    @ResponseBody
    public Result findByKeyword(@RequestParam(required = false,defaultValue = "")String keyword){

        List<Account_businessInfo> account_businessInfos=business.findByKeyword(keyword);

        if(account_businessInfos.size()!=0){
            HashMap<String,List<Account_businessInfo>> hashMap=new HashMap<>();
            hashMap.put("account_businessInfos",account_businessInfos);
            return Result.buildData(202,"查询成功",hashMap);
        }else {
            return Result.build(400,"查无数据");
        }
    }
    @ApiOperation(value="下架跑腿业务")
    @ResponseBody
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable int id,HttpSession session){
        Integer accountId=Integer.parseInt(session.getAttribute("loginId").toString());
        if(session.getAttribute("loginId")==null){
            return Result.build(400,"请登录");
        }
        Business_info business_info=new Business_info();
        business_info.setAccountId(accountId);
        business_info.setStatus(1);
        business_info.setId(id);
        if(business.update(business_info)>0){
            return Result.build(200,"下架成功");
        }else {
            return Result.build(400,"请求失败");
        }
    }
    @ApiOperation(value ="修改跑腿业务" )
    @ResponseBody
    @PutMapping("")
    public Result update(Business_info business_info ,HttpSession session){
        Integer accountId=Integer.parseInt(session.getAttribute("loginId").toString());
        if(session.getAttribute("loginId")==null){
            return Result.build(400,"请登录");
        }
        business_info.setAccountId(accountId);
        if(business.update(business_info)>0){
            return Result.build(200,"修改成功");
        }else {
            return Result.build(400,"修改失败");
        }
    }
    @ApiOperation(value="根据accountId查询个人发布的业务")
    @ResponseBody
    @GetMapping("/accountId")
    public Result selectByAccountId(HttpSession session){
        Integer accountId=Integer.parseInt(session.getAttribute("loginId").toString());
        if(session.getAttribute("loginId")==null){
            return Result.build(400,"请登录");
        }
        List<Account_businessInfo> account_businessInfos=business.selectByAccountId(accountId);
        if(account_businessInfos.size()>0){
            HashMap<String,List<Account_businessInfo>> hashMap=new HashMap<>();
            hashMap.put("account_businessInfos",account_businessInfos);
            return Result.buildData(202,"查询成功",hashMap);
        }else {
            return Result.build(200,"查询结果为空");
        }
    }

    @ResponseBody
    @GetMapping("/ByOrder/accountId/{accountId}")
    public Result selectByOrderAccountId(@PathVariable int accountId){
        List<Business_info> business_infos=business.selectByOrder(accountId);
        if(business_infos.size()>0){
            return Result.buildData(202,"查询成功",business_infos);
        }else {
            return Result.build(200,"查询结果为空");
        }
    }

    @GetMapping("/findAllUser")
    @ResponseBody
    public List<Account_businessInfo>  findAllUser(){
//        User user = new User();
        Account_businessInfo account_businessInfo=new Account_businessInfo();
        int page=1;//当前页
        int pageSize=2;//页面接收数据大小
        IPage<Account_businessInfo> iPage = business.selectPage(account_businessInfo,page,pageSize);
        iPage.getRecords();
        return   iPage.getRecords();
    }
    @GetMapping("/ByOrder/{id}")
    @ResponseBody
    public Result selectById(@PathVariable int id){
        Business_info business_info=business.findByOrder(id);

        if(business_info!=null){
            return Result.buildData(202,"查询成功",business_info);
        }else {
            return Result.build(400,"查无此业务");
        }
    }

    @ResponseBody
    @PutMapping("/update")
    public Result updateBusiness(@RequestBody Business_info business_info ,HttpSession session){

        if(business.update(business_info)>0){
            return Result.build(200,"修改成功");
        }else {
            return Result.build(400,"修改失败");
        }
    }
}
